<%@page import="is.web.xdumper.client.XdumperClientSessionDeleteForm"%>
<%@page import="java.io.File"%>
<%@page import="is.bean.entity.xdumper.session.XdumperSessionBeanRemote"%>
<%@page import="java.util.Collection"%>
<%@page import="is.bean.entity.xdumper.session.XdumperSessionBeanRemoteHome"%>
<%@page import="is.web.xdumper.client.table.XdumperSessionTable"%>
<%@page import="nullutil.NullUtil"%>
<%@page import="is.web.htgen.Message"%>
<%@page import="javax.ejb.FinderException"%>
<%@page import="is.bean.entity.preference.Preference"%>
<%@page import="is.bean.entity.preference.PreferenceBeanRemote"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="is.bean.entity.preference.PreferenceBeanRemoteHome"%>
<%@page import="javax.naming.InitialContext"%>

<%@page import="is.web.xdumper.client.XdumperClient"%>

<%
    String importHtml = "generation error";
    String sessionHtml = "generation error";
    String dapStored = "";
    String msgHtml = "";

    try
    {
        /* Message */
        if (NullUtil.isZeroPositiveInteger(request.getParameter("i")))
        {
            msgHtml = Message.info(request.getParameter("i"));
        }
        else if (NullUtil.isZeroPositiveInteger(request.getParameter("w")))
        {
            msgHtml = Message.warning(request.getParameter("w"));
        }
        else if (NullUtil.isZeroPositiveInteger(request.getParameter("e")))
        {
            msgHtml = Message.error(request.getParameter("e"));
        }

        /* Deployed Application Path */
        Object pbRef = new InitialContext().lookup("ejb/PreferenceBean");
        PreferenceBeanRemoteHome preferenceHome =
                (PreferenceBeanRemoteHome) PortableRemoteObject.narrow(pbRef, PreferenceBeanRemoteHome.class);

        String dapPrefName = "dap";
        PreferenceBeanRemote preferenceRemote = null;
        Preference dap = null;
        boolean dapBeanExists = preferenceHome.entityExist(dapPrefName);

        if (dapBeanExists)
        {
            preferenceRemote = preferenceHome.findByPrimaryKey(dapPrefName);
            dap = preferenceRemote.getData();
            dapStored = dap.getValue();
        }
        else
        {
            msgHtml = Message.warning("7");
        }

        if ("setdap".equals(request.getParameter("submit")) && request.getParameter("dapParam") != null)
        {
            String dapParam = request.getParameter("dapParam");

            if (dapParam.endsWith("/") || dapParam.endsWith("\\"))
            {
                File appDir = new File(dapParam + "5_isbdx/");

                if (appDir.exists())
                {
                    if (dapBeanExists)
                    {
                        dap.setValue(dapParam);

                        preferenceRemote.setData(dap);
                    }
                    else
                    {
                        dap = new Preference();
                        dap.setName(dapPrefName);
                        dap.setValue(dapParam);

                        preferenceHome.create(dap);
                    }
                }
                else
                {
                    /* Check app dir available */
                    response.sendRedirect("?w=10");
                }
            }
            else
            {
                /* No slash */
                response.sendRedirect("?w=8");
            }

            /* Path set */
            response.sendRedirect("?i=5");
        }

        /* Session */
        Object xdsRef = new InitialContext().lookup("ejb/XdumperSessionBean");
        XdumperSessionBeanRemoteHome sessionHome =
                (XdumperSessionBeanRemoteHome) PortableRemoteObject.narrow(xdsRef,
                                                                           XdumperSessionBeanRemoteHome.class);
        Collection<XdumperSessionBeanRemote> sessionRemotes = sessionHome.findByRange(-1, 0);

        sessionHtml = XdumperSessionTable.generate(sessionRemotes);

        if ("delete".equals(request.getParameter("act")))
            {
                XdumperClientSessionDeleteForm form = new XdumperClientSessionDeleteForm();
                form.setSessionHome(sessionHome);
                Integer id = Integer.parseInt(request.getParameter("id"));

                sessionHtml = form.genDelete(id);

                if (request.getMethod().equals("POST"))
                {
                    sessionHome.remove(id);
                    response.sendRedirect("?i=1");
                }
            }

        /* Import */

        XdumperClient client = new XdumperClient();
        client.setRequest(request);

        importHtml = client.stepImport();
//        html = client.(request);
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }

%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title><%=is.web.htgen.Locale.TITLE%></title>
        <link rel="stylesheet" href="../style/common.css">
        <link rel="stylesheet" href="../style/msg.css">
        <link rel="stylesheet" href="../style/xdumper.css">
        <script src="../script.js" type="text/javascript"></script>
    </head>
    <body onload="openTab('dap', 'dap');">

        <h1><a href=".."><img src="../img/go-home_32.png" class="linkIcon"></a>| Xdumper</h1>

        <%=msgHtml%>

        <div class="spoiler" onclick="toggleDisplay('dap');">Deployed application path</div>
        <div class="spoiled" id="dap" style="display: none;">
            <form method="post">
                <div class="advice">
                    Set path to directory containing deployed '5_isbdx/'. Path must end with slash.
                </div>
                <input name="dapParam" type="text" value="<%=dapStored%>" style="width: 70%;"/>
                <button name="submit" type="submit" value="setdap">Set</button>
            </form>
        </div>

        <div class="spoiler" onclick="toggleDisplay('importHtml');">Import</div>
        <div class="spoiled" id="importHtml">
            <%=importHtml%>
        </div>

        <div class="spoiler" onclick="toggleDisplay('sessionHtml');">Sessions</div>
        <div class="spoiled" id="sessionHtml">
            <%=sessionHtml%>
        </div>

    </body>
</body>
</html>